IF EXISTS (select * from dbo.sysobjects where id = object_id(N'[dbo].UpdateUserAccessToClinicalTrialBudget') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].UpdateUserAccessToClinicalTrialBudget
GO

CREATE PROCEDURE dbo.UpdateUserAccessToClinicalTrialBudget (
		@domainUserId			INTEGER,
		@budgetId				INTEGER,
		@readPending			INTEGER,
		@readFinal				INTEGER,
		@modifyPending			INTEGER
)
AS
BEGIN

SET NOCOUNT ON

DECLARE @roleBitmask int
DECLARE @pendingBudgetReadAccessBit INT
DECLARE @finalBudgetReadAccessBit INT
DECLARE @pendingBudgetModifyAccessBit INT
DECLARE @resourceId int
DECLARE @budgetResourceTypeId int
DECLARE @str varchar(4000)

SET @pendingBudgetReadAccessBit = 8192
SET @finalBudgetReadAccessBit = 4096
SET @pendingBudgetModifyAccessBit = 32768
SET @budgetResourceTypeId = 3

SELECT @resourceId = FwkResourceId 
FROM ClinicalTrialBudget 
WHERE Id = @budgetId

DECLARE @piId INT
DECLARE @originatorId INT

SELECT @piId = PIPersonId FROM ClinicalTrialBudget WHERE Id = @budgetId
SELECT @originatorId = OriginatorPersonId FROM ClinicalTrialBudget WHERE Id = @budgetId

IF ((@piID != @domainUserId) and (@originatorId != @domainUserId))
	BEGIN		
		SELECT @roleBitmask = frdum.AccessRightBitmask							
							from FwkResource_DomainUser_Map frdum
							inner join FwkResource fr
							on fr.Id = Frdum.FwkResourceId
							inner join ClinicalTrialBudget ctb
							on ctb.FwkResourceId = frdum.FwkResourceId
							where frdum.FwkDomainUserId = @domainUserId 
							and frdum.FwkResourceId=@resourceId 
							and fr.FwkResourceTypeId=@budgetResourceTypeId
							and ctb.Id = @budgetId						

		if (@roleBitmask is null) set @roleBitmask = 0

		if ((@modifyPending = 1) and (@readPending =0)) set @readPending = 1
		
		IF (((@roleBitmask & @pendingBudgetReadAccessBit) != @pendingBudgetReadAccessBit) and @readPending = 1)
		BEGIN		
			set @roleBitmask = @roleBitmask + @pendingBudgetReadAccessBit		
		END
		ELSE IF (((@roleBitmask & @pendingBudgetReadAccessBit) = @pendingBudgetReadAccessBit) and @readPending = 0)
		BEGIN
			set @roleBitmask = @roleBitmask - @pendingBudgetReadAccessBit
		END
		IF (((@roleBitmask & @finalBudgetReadAccessBit) != @finalBudgetReadAccessBit) and @readFinal = 1)
		BEGIN
			set @roleBitmask = @roleBitmask + @finalBudgetReadAccessBit
		END
		ELSE IF (((@roleBitmask & @finalBudgetReadAccessBit) = @finalBudgetReadAccessBit) and @readFinal = 0)
		BEGIN
			set @roleBitmask = @roleBitmask - @finalBudgetReadAccessBit
		END
		IF (((@roleBitmask & @pendingBudgetModifyAccessBit) != @pendingBudgetModifyAccessBit) and @modifyPending = 1)
		BEGIN
			set @roleBitmask = @roleBitmask + @pendingBudgetModifyAccessBit
		END
		ELSE IF (((@roleBitmask & @pendingBudgetModifyAccessBit) = @pendingBudgetModifyAccessBit) and @modifyPending = 0)
		BEGIN
			set @roleBitmask = @roleBitmask - @pendingBudgetModifyAccessBit
		END				

		exec ChangeDirectResourceAccess @domainUserId,@resourceId,@roleBitmask		
	END		
END